programming4us
           
 
 
Programming

iPad SDK : Keyboard Extensions and Replacements (part 4) - Creating the Calculator

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/2/2010 2:54:21 PM
2.3. Creating the Calculator

We're now going to define our app's view controller, which does the actual work of being a calculator.

Open the ClacHsilopViewController.h file, and add the instance variables shown in the following listing. We also declare it to be a delegate of the InputView class.

//  ClacHsilopViewController.h
#import <UIKit/UIKit.h>
#import "InputView.h"
@interface ClacHsilopViewController : UIViewController <InputViewDelegate> {
IBOutlet InputView *inputView;
IBOutlet UITableView *stackTableView;
NSNumberFormatter *decimalFormatter;
NSMutableArray *stack;
}
@end


Before implementing that class, let's set up the GUI. Open ClacHsilopViewController.xib in Interface Builder. Select the main view and set its background color to a darker gray, just to make our other components stand out. Then use the Library to find an InputView, and drag it to the top of the view. Our table will eventually display the stack in a right-justified column, so right-justify the InputView as well, using the attribute inspector.

Next, drag out a UITableView, making it fill most of the view, as shown in Figure 6. The table doesn't need to extend to the bottom of the view, since the customized inputView will be appearing on top of it.

Connect the table view's dataSource and delegate outlets to the File's Owner proxy icon, as well as the InputView's ivDelegate outlet. Then connect the inputViewstackTableView outlets to the appropriate objects. Save your work now, and switch back to Xcode. and

Figure 6. Text and a table

Open ClacHsilopViewController.m. This class will have two primary functions: presenting the contents of the stack in a table view and handling the actual calculator functionality in response to the user working the controls.

//  ClacHsilopViewController.m
#import "ClacHsilopViewController.h"
@implementation ClacHsilopViewController
- (void)viewDidLoad {
[super viewDidLoad];
stack = [[NSMutableArray alloc] init];
decimalFormatter = [[NSNumberFormatter alloc] init];
decimalFormatter.numberStyle = NSNumberFormatterDecimalStyle;
[stackTableView reloadData];
[inputView becomeFirstResponder];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)o {
return YES;
}
- (void)dealloc {
[stack release];
[decimalFormatter release];
[super dealloc];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)s {


return [stack count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier] autorelease];
}
cell.detailTextLabel.text = [decimalFormatter stringFromNumber:[stack
objectAtIndex:indexPath.row]];

return cell;
}
- (void)handleError {
// in case of an error, push the current number
// onto the stack instead of just tossing it
NSDecimalNumber *inputNumber = [NSDecimalNumber
decimalNumberWithString:inputView.text];
[stack insertObject:inputNumber atIndex:0];
inputView.text = @"Error";
}
- (void)doEnter {
NSDecimalNumber *inputNumber = [NSDecimalNumber
decimalNumberWithString:inputView.text];
[stack insertObject:inputNumber atIndex:0];
[stackTableView reloadData];
inputView.text = @"0";
}
- (void)doDecimalArithmetic:(SEL)method {
if ([stack count] > 0) {
NSDecimalNumber *inputNumber = [NSDecimalNumber
decimalNumberWithString:inputView.text];
NSDecimalNumber *stackNumber = [stack objectAtIndex:0];
NSDecimalNumber *result = [stackNumber performSelector:method
withObject:inputNumber];
inputView.text = [decimalFormatter stringFromNumber:result];
[stack removeObjectAtIndex:0];
} else {
[self handleError];
}
[stackTableView reloadData];
}
- (void)doTaggedAction:(ActionTag)tag forInputView:(InputView *)iv {
switch (tag) {
case ActionEnter:
[self doEnter];
break;
case ActionDivide:
[self doDecimalArithmetic:@selector(decimalNumberByDividingBy:)];
break;
case ActionMultiply:
[self doDecimalArithmetic:
@selector(decimalNumberByMultiplyingBy:)];


break;
case ActionSubtract:
[self doDecimalArithmetic:@selector(decimalNumberBySubtracting:)];
break;
case ActionAdd:
[self doDecimalArithmetic:@selector(decimalNumberByAdding:)];
break;
default:
break;
}
}
@end

That's it! With this code in place, you should now be able to build and run the app, see the GUI appear, and immediately have the RPN input keypad at your disposal.

Other -----------------
- iPad SDK : New Input Methods - Gesture Recognition
- iPad SDK : New Input Methods - Menu Additions
- iPad SDK : Implementing an About Panel in a Modal Way (part 2)
- iPad SDK : Implementing an About Panel in a Modal Way (part 1) - Creating the Modal Web View Controller
- Parallel Programming with Microsoft .Net : Dynamic Task Parallelism - Variations
- Keyword Research Tools (part 7) - comScore Marketer
- Keyword Research Tools (part 6)
- Keyword Research Tools (part 5)
- Keyword Research Tools (part 4)
- Keyword Research Tools (part 3)
- Keyword Research Tools (part 2)
- Keyword Research Tools (part 1) - Keyword Research Data from the Engines
- The Art of SEO : Traditional Approaches: Domain Expertise, Site Content Analysis
- The Art of SEO : The Theory Behind Keyword Research
- jQuery 1.3 : Headline rotator (part 7)
- jQuery 1.3 : Headline rotator (part 6)
- jQuery 1.3 : Headline rotator (part 5) - Pause on hover
- jQuery 1.3 : Headline rotator (part 4) - The headline rotate function
- jQuery 1.3 : Headline rotator (part 3) - Setting up the rotator
- jQuery 1.3 : Headline rotator (part 2) - Retrieving the feed
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us